﻿//-----------------------------------------------------------------------
// <copyright file="SqlDbHandler.cs">
//     Copyright (c) Wolf166 - BGF Framework
// </copyright>
// <Autor>Wolf166 - BGF Framework</Autor>
//-----------------------------------------------------------------------
namespace BrowserGameFramework.Account
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Security.Cryptography;
    using System.Text;
    using System.Xml.XPath;
    using BrowserGameFramework.Common;
    using BrowserGameFramework.DBConnector;
    using BrowserGameFramework.DBConnector.SqlServer;


    /// <summary>
    /// This abstract class handels the Login and
    /// provice some authentication specific functions
    /// </summary>
    public abstract class BaseLoginHandler
    {
        #region abstract

        /// <summary>
        /// Login user with given credentials
        /// </summary>
        /// <param name="user">LoginUser as string</param>
        /// <param name="pass">Pass as string</param>
        /// <returns></returns>
        public abstract IUserprincipal Login(string user, string pass);

        #endregion abstract

        /// <summary>
        /// Gets a md5 hash for a string
        /// </summary>
        /// <param name="pass">input string</param>
        /// <returns>md5 hash as string</returns>
        public string GetMd5FromString(string pass)
        {
            return GetMd5FromString(pass, Encoding.Default);
        }

        /// <summary>
        /// Gets a MD5 hash for password
        /// </summary>
        /// <param name="pass">input string</param>
        /// <param name="encode">encoding</param>
        /// <returns>md5 hash as string</returns>
        public string GetMd5FromString(string pass, Encoding encode)
        {
            if ((string.IsNullOrEmpty(pass)))
            {
                return string.Empty;
            }

            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] textToHash = Encoding.Default.GetBytes(pass);
            byte[] result = md5.ComputeHash(textToHash);

            return System.BitConverter.ToString(result); 

        }
    }
}
